﻿@using CSF.Utility.Mvc;

@functions {
    protected const string NAV_ON_CLASS = "nav_on";
    protected const string NAV_OFF_CLASS = "nav_off";
    
    protected string GetNavClass(string path)
    {
        string area = "";
        var values = ViewContext.RouteData.DataTokens;
        if (values.ContainsKey("area"))
        {
            area = Convert.ToString(values["area"]);
        }
        string prefix = "/" + area;
        if (ViewContext.RouteData.Values.ContainsKey("appCode"))
        {
            prefix += "/" + Convert.ToString(ViewContext.RouteData.Values["appCode"]);
        }
        if (UrlUtility.IsStartWithPath(path, prefix))
        {
            return NAV_ON_CLASS;
        }
        return NAV_OFF_CLASS;
    }
    
    protected SiteMapNode[] GetMenuItems()
    {
        var root = SiteMap.RootNode;
        var nodes = root.ChildNodes;
        
        if (ViewContext.RouteData.Values.ContainsKey("appCode"))
        {
            var appCode = Convert.ToString(ViewContext.RouteData.Values["appCode"]);
            
            foreach(var node in nodes)
            {
                if (((SiteMapNode)node).Description == Convert.ToString(ViewContext.RouteData.Values["appCode"]))
                {
                    nodes = ((SiteMapNode)node).ChildNodes;
                    break;
                }
            }
        }

        SiteMapNode[] result = new SiteMapNode[nodes.Count + 1];
        result[0] = root;
        int i = 1;
        foreach (var node in nodes)
        {
            result[i++] = (SiteMapNode)node;
        }

        return result;
    }
}
@helper DisplayMainMenuItem(SiteMapNode node) {
    string path = node.Url;
    if (!string.IsNullOrEmpty(Request["SessionId"]))
    {
        path = UrlUtility.AppendParameter(path, "SessionId", Request["SessionId"]);
    }
    string cssclass = GetNavClass(path);
    <li>
        <a href="@Url.Content(path)" class="@cssclass">@node.Title</a>
    </li>
}
@{
    SiteMapNode[] nodes = GetMenuItems();
}
<div id="menu_out">
    <div id="menu_in">
        <div id="menu">
            <ul id="nav">
            @foreach (var node in nodes)
            {
                @DisplayMainMenuItem(node);
                <li class="menu_line"></li>
            }
            </ul>
        </div>
    </div>
</div>
<script type="text/javascript">
    //    var currtag = $('#menu #nav a[class="@NAV_ON_CLASS"]');
    $('#menu #nav a').each(function () {
        var text = $(this).text();
        $(this).html('<span>' + text + '</span>');
    });
    $('#menu #nav a[class!="@NAV_ON_CLASS"]').mouseenter(function () {
        $(this).removeClass('@NAV_OFF_CLASS');
        $(this).addClass('@NAV_ON_CLASS');
    }).mouseleave(function () {
        $(this).removeClass('@NAV_ON_CLASS');
        $(this).addClass('@NAV_OFF_CLASS');
    });

</script>
